package com.ivan.stu.notetool.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.github.zackratos.ultimatebar.UltimateBar;
import com.ivan.stu.notetool.R;
import com.ivan.stu.notetool.adapter.MyPagerAdapter;
import com.ivan.stu.notetool.model.NotifyFinishEvent;
import com.ivan.stu.notetool.utils.ActivityCollectorUtils;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class ShowImageActivity extends AppCompatActivity implements MyPagerAdapter.onSingleClickListener{
    private ViewPager viewPager;  //声明viewpage
    private List<View> listViews = null;  //用于获取图片资源
    private int index = 0;   //获取当前点击的图片位置
    private MyPagerAdapter adapter;   //ViewPager的适配器
    private ArrayList<Bitmap> bmp = null;
    private int position;
    private List<String> imageList;
    private int imageDone = 0;
    private TextView tv_index;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  //去除标题栏
        setContentView(R.layout.activity_show_image);    //绑定布局
        UltimateBar.Companion.with(this).applyNavigation(false).create().transparentBar();

        inigView();
        initData();

        if (!EventBus.getDefault().isRegistered(this)) {

            EventBus.getDefault().register(this);

        }
       //     ActivityCollectorUtils.addActivity(this);


    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void notifyFinish(NotifyFinishEvent event){

        if (event.getFinishSigin() == 1){

            finish();
        }
    }

    public static void openImageActivity(Context context,int position , ArrayList<String> imageList){

        Intent intent = new Intent(context,ShowImageActivity.class);
        intent.putExtra("imageList",imageList);
        intent.putExtra("position",position);
        context.startActivity(intent);

    }

    private void inigView() {


        viewPager = (ViewPager) findViewById(R.id.show_view_pager);  //绑定viewpager的id
        tv_index = findViewById(R.id.tv_index);


    }

    private void initData() {
        listViews = new ArrayList<View>();   //初始化list
        position = getIntent().getIntExtra("position",0);
        imageList = (List<String>) getIntent().getSerializableExtra("imageList");

    //    LogUtils.d("position====================="+position);

        inint();
    }

    private void inint() {

        if (imageList != null && imageList.size() > 0){

            tv_index.setText((position+1)+"/"+imageList.size());
            adapter = new MyPagerAdapter(imageList,ShowImageActivity.this);
            adapter.setOnSingleClickLisner(this);
            viewPager.setAdapter(adapter);
            viewPager.setOnPageChangeListener(new PageChangeListener()); //设置viewpager的改变监听
            //截取intent获取传递的值

            adapter.notifyDataSetChanged();
            viewPager.setCurrentItem(position);

      /*     getImage(new ImageCompleteCallBack() {
                @Override
                public void onCpmplete() {  //异步加载完成时展示图片
                    adapter = new MyPagerAdapter(listViews,ShowImageActivity.this);
                    viewPager.setAdapter(adapter);
                    viewPager.setOnPageChangeListener(new PageChangeListener()); //设置viewpager的改变监听
                    //截取intent获取传递的值

                    adapter.notifyDataSetChanged();
                    viewPager.setCurrentItem(position);    //viewpager显示指定的位置

                }
            });*/
        }
    }

    private void getImage(final ImageCompleteCallBack callBack) {

        for (int i = 0; i < imageList.size(); i++) {  //for循环将试图添加到list中

            final View view = LayoutInflater.from(getApplicationContext()).inflate(
                    R.layout.view_pager_item, null);   //绑定viewpager的item布局文件
//                ImageView iv = (ImageView) view.findViewById(R.id.view_image);   //绑定布局中的id
//                iv.setImageBitmap(bmp.get(i));   //设置当前点击的图片

            final SubsamplingScaleImageView iv = (SubsamplingScaleImageView) view.findViewById(R.id.view_image);   //绑定布局中的id

            final String url = imageList.get(i);
            iv.setTag(url);
            //   iv.setImage(ImageSource.bitmap(bmp.get(i)));

            Glide.with(this).load(imageList.get(i)).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {

                    Bitmap bitmap = ((BitmapDrawable)resource).getBitmap();

                    if (iv.getTag().equals(url)){

                        iv.setImage(ImageSource.bitmap(bitmap));
                        listViews.add(view);

                        imageDone++;
                    }

                    if (imageDone == imageList.size()){

                        if (callBack!=null){

                            callBack.onCpmplete();

                        }
                    }

                }
            });

      /*      ImageUtil.getBitmaoByPath(imageList.get(i), new ImageUtil.ImageCallBack() {
                @Override
                public void onFinish(final Bitmap bitmap) {

                    Global.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            iv.setImage(ImageSource.bitmap(bitmap));
                            listViews.add(view);

                            imageDone++;

                            if (imageDone == imageList.size()){

                                if (callBack!=null){

                                    callBack.onCpmplete();

                                }
                            }
                        }
                    });
                }
            });*/

            /**
             * 图片的长按监听
             * */
            iv.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    //弹出提示，提示内容为当前的图片位置
              //      Toast.makeText(ShowImageActivity.this, "这是第" + (index + 1) + "图片", Toast.LENGTH_SHORT).show();
                    return false;
                }
            });
        }
    }

    @Override
    public void onSingleClick() {

        finish();

    }


    public interface ImageCompleteCallBack{

        void onCpmplete();
    }


    /**
     * pager的滑动监听
     * */
    private class PageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int arg0) {
            index = arg0;

         //   Log.e("当前位置是：",index+"");
            tv_index.setText((index+1)+"/"+imageList.size());
        }

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
